package com.aep.provisioning.annotations.controller;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.aep.provisioning.controller.form.BillingTypeForm;
import com.aep.provisioning.service.WebFacade;


public class EditBillingTypeController {

	private transient static Logger logger = Logger.getLogger(EditBillingTypeController.class);
	private WebFacade webFacade;

	
	@RequestMapping(value = "/internal/editBillingType",method=RequestMethod.GET)
	public @ModelAttribute("billingTypeForm") BillingTypeForm viewForm(){
		logger.info(" =========== VIEWFORM ========== " + webFacade);
		BillingTypeForm billingTypeForm = new BillingTypeForm();
		billingTypeForm.setMessageProduct("INTRO CONTROLLER");
		return billingTypeForm ;
	}
	
	@RequestMapping(value = "/internal/editBillingType",method=RequestMethod.POST,params = "action=save")
	public 	@ModelAttribute("billingTypeForm") ModelAndView saveBillingType(@ModelAttribute("billingTypeForm") @Valid BillingTypeForm billingTypeForm,BindingResult result, SessionStatus status){
		String accountId = "0";
		ModelAndView mav = new ModelAndView("billingTypeForm");
		if ( result.hasErrors() ) {
			mav.setViewName("internal/editBillingType");
			return mav;
		}else{
			logger.info(" =========== saveBillingType ==========");
		}
		accountId = billingTypeForm.getAccountProductId().toString();
		status.setComplete();
		mav.setViewName(getUrl(accountId));
		return mav;
		
	}
	
    private String getUrl(String idAccountString){
    	return "redirect:seeDetail.html?idAccount=" + idAccountString;
    }
	
	
}
